<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Set</title>
</head>
<body>
    <script>
        // 定义
        let fruits = new Set([
            'apple',
            'orange',
            'banana'
        ]);
        console.log("fruits: ", fruits);

        // add
        fruits.add('mango');
        fruits.add('mango');
        console.log("after add: ", fruits);

        // delete
        fruits.delete('banana');
        console.log("after delete: ", fruits);

        // has
        console.log("has apple: ", fruits.has('apple'));
        console.log("has banana: ", fruits.has('banana'));

        // size
        console.log("size: ", fruits.size);

        // to array
        let arr = Array.from(fruits);
        console.log("arr: ", arr);

        // to array
        let arr2 = [...fruits];
        console.log("arr2: ", arr2);

        // 展开可迭代对象，可使用中文，每个中文一个元素
        let web = "www.baidu.com";
        let webArray = [...web];
        console.log("webArray: ", webArray);

        // for...of 遍历
        for(let item of fruits) {
            console.log("for...of: ", item);
        }

        // forEach 遍历
        fruits.forEach((item, index) => {
            console.log("forEach: ", item, ", index: ", index);
        });

        // clear
        fruits.clear();
        console.log("clear set: ", fruits);

        // array to set，数组去重
        let numArr = [1, 2, 3, 1, 3, 4, 5, 6, 8, 7, 6];
        let numSet = new Set(numArr);
        console.log("to set: ", numSet);
        console.log("to set: ", Array.from(numSet).sort());

    </script>
</body>
</html>